package com.xmbandu.app.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.http.SslError;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xmbandu.app.R;
import com.xmbandu.app.base.BaseActivity;

/**
 * wmq
 */

public class H5PayActivity extends BaseActivity {


    private static final String URL = "URL";
    private static final String COLOR_STR = "COLOR";
    private static final String TITLE = "TITLE";

    private ImageView ssp_web_back;
    private TextView ssp_web_title;
    private ProgressBar ssp_web_pb;
    private WebView ssp_web_wv;
    private RelativeLayout ssp_web_top;

    private String url;


    public static void intoSspCommonWeb(Context context, String url, String title) {
        Intent intent = new Intent(context, H5PayActivity.class);
        intent.putExtra(URL, url);
        intent.putExtra(TITLE, title);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }
    public static void intoSspCommonWeb(Context context, String url) {
        Intent intent = new Intent(context, H5PayActivity.class);
        intent.putExtra(URL, url);
        intent.putExtra(TITLE, "");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_web;
    }

    @Override
    public void initViews() {
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        ssp_web_back = $(R.id.ssp_web_back);
        ssp_web_wv = $(R.id.ssp_web_wv);
        ssp_web_title = $(R.id.ssp_web_title);
        ssp_web_pb = $(R.id.ssp_web_pb);
        ssp_web_top = $(R.id.ssp_web_top);

    }

    @Override
    public void initListener() {
        setOnClick(ssp_web_back);
    }

    @Override
    public void initData() {
        boolean isTop = getIntent().getBooleanExtra("isHaveTop", true);

        if (isTop)
            ssp_web_top.setVisibility(View.VISIBLE);
        else
            ssp_web_top.setVisibility(View.GONE);

        url = getIntent().getStringExtra(URL);
        String colorStr = getIntent().getStringExtra(COLOR_STR);
        if (!TextUtils.isEmpty(colorStr)) {
            try {
                if ("#ffffff".equals(colorStr)) {
                    ssp_web_back.setBackgroundResource(R.mipmap.ssp_icon_left_back2);
                    ssp_web_title.setTextColor(Color.parseColor("#333333"));

                }
                int color = Color.parseColor(colorStr);
                ssp_web_top.setBackgroundColor(color);
            } catch (Exception e) {

            }
        }
        String book_list = getIntent().getExtras().getString(TITLE, "书城");
        if (!TextUtils.isEmpty(book_list))
        {
            ssp_web_title.setText(book_list);
        }

        ssp_web_wv.getSettings().setDomStorageEnabled(true);
        ssp_web_wv.getSettings().setJavaScriptEnabled(true);
        ssp_web_wv.getSettings().setAllowUniversalAccessFromFileURLs(true);
        ssp_web_wv.loadUrl(url);
        ssp_web_wv.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return super.shouldOverrideUrlLoading(view, url);
            }

//            @Override
//            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
//                view.loadUrl(request.toString());
//                return true;
//            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                if (error.getPrimaryError() == SslError.SSL_DATE_INVALID
                        || error.getPrimaryError() == SslError.SSL_EXPIRED
                        || error.getPrimaryError() == SslError.SSL_INVALID
                        || error.getPrimaryError() == SslError.SSL_UNTRUSTED) {
                    handler.proceed();
                } else {
                    handler.cancel();
                }

                super.onReceivedSslError(view, handler, error);
            }
        });
//        ssp_web_wv.setWebChromeClient(new WebChromeClient() {
//
//            @Override
//            public void onReceivedTitle(WebView view, String title) {
//                super.onReceivedTitle(view, title);
//                ssp_web_title.setText(title);
//            }
//
//
//            @Override
//            public void onProgressChanged(WebView view, int newProgress) {
//                if (newProgress == 100) {
//                    ssp_web_pb.setVisibility(View.GONE);//加载完网页进度条消失
//                } else {
//                    ssp_web_pb.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
//                    ssp_web_pb.setProgress(newProgress);//设置进度值
//                }
//
//            }
//        });
        Log.e("weburl", "initData: " + url);
    }

    @Override
    public void processClick(View v) {
        int i = v.getId();
        if (i == R.id.ssp_web_back) {
            if (ssp_web_wv.canGoBack()) {
                ssp_web_wv.goBack();
            } else {
                finish();
            }
        }
    }
}
